package cn.edu.cdu.farmdeal.service.impl;

import cn.edu.cdu.farmdeal.domain.Shop;
import cn.edu.cdu.farmdeal.mapper.ShopMapper;
import cn.edu.cdu.farmdeal.service.CartService;
import org.omg.PortableInterceptor.INACTIVE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Author: tupeng
 * @Date: 2020/3/15
 **/
@Service
public class CartServiceImpl implements CartService {
    @Autowired
    private ShopMapper shopMapper;

    @Override
    public List<Shop> getCart(String userName) {
        return shopMapper.getCart(userName);
    }

    @Override
    public int deleteCart(String id) {
        String shopId = shopMapper.getShopId(id);
        shopMapper.deleteCart(id);
        int count = shopMapper.getConuntPro(shopId);
        if (count == 0) {
            //删除shop
            shopMapper.deleteShop(shopId);
        }
        return 1;
    }

    @Override
    public int reduceCart(String id,String price) {
         //更新数量
         shopMapper.reduceCart(id);
         //更新总价
         shopMapper.reduceCartPrice(Integer.parseInt(id),price);
         return 1;
    }

    @Override
    public int plusCart(String id,String price) {
        shopMapper.plusCart(id);
        shopMapper.plusCartPrice(Integer.parseInt(id),price);
        return 1;
    }

}
